{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width=\"200\" src=\"https://mmlspark.blob.core.windows.net/graphics/emails/vw-blue-dark-orange.svg\" />\n",
    "\n",
    "# Multi-class Classification using Vowpal Wabbit\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Read dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyspark.sql.types as T\n",
    "from pyspark.sql import functions as F\n",
    "\n",
    "schema = T.StructType(\n",
    "    [\n",
    "        T.StructField(\"sepal_length\", T.DoubleType(), False),\n",
    "        T.StructField(\"sepal_width\", T.DoubleType(), False),\n",
    "        T.StructField(\"petal_length\", T.DoubleType(), False),\n",
    "        T.StructField(\"petal_width\", T.DoubleType(), False),\n",
    "        T.StructField(\"variety\", T.StringType(), False),\n",
    "    ]\n",
    ")\n",
    "\n",
    "df = (\n",
    "    spark.read.format(\"csv\")\n",
    "    .option(\"header\", True)\n",
    "    .schema(schema)\n",
    "    .load(\"wasbs://publicwasb@mmlspark.blob.core.windows.net/iris.txt\")\n",
    ")\n",
    "# print dataset basic info\n",
    "print(\"records read: \" + str(df.count()))\n",
    "print(\"Schema: \")\n",
    "df.printSchema()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "display(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Use VowpalWabbitFeaturizer to convert data features into vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyspark.ml.feature import StringIndexer\n",
    "\n",
    "from synapse.ml.vw import VowpalWabbitFeaturizer\n",
    "\n",
    "indexer = StringIndexer(inputCol=\"variety\", outputCol=\"label\")\n",
    "featurizer = VowpalWabbitFeaturizer(\n",
    "    inputCols=[\"sepal_length\", \"sepal_width\", \"petal_length\", \"petal_width\"],\n",
    "    outputCol=\"features\",\n",
    ")\n",
    "\n",
    "# label needs to be integer (0 to n)\n",
    "df_label = indexer.fit(df).transform(df).withColumn(\"label\", F.col(\"label\").cast(\"int\"))\n",
    "\n",
    "# featurize data\n",
    "df_featurized = featurizer.transform(df_label).select(\"label\", \"features\")\n",
    "\n",
    "display(df_featurized)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Split the dataset into train and test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train, test = df_featurized.randomSplit([0.8, 0.2], seed=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Model Training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from synapse.ml.vw import VowpalWabbitClassifier\n",
    "\n",
    "\n",
    "model = (\n",
    "    VowpalWabbitClassifier(\n",
    "        numPasses=5,\n",
    "        passThroughArgs=\"--holdout_off --oaa 3 --holdout_off --loss_function=logistic --indexing 0 -q ::\",\n",
    "    )\n",
    "    .setNumClasses(3)\n",
    "    .fit(train)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Model Prediction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictions = model.transform(test)\n",
    "\n",
    "display(predictions)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
